package auctionhaus
//Authors: Paul Kleczka, Adam Backstrom

import grails.test.mixin.TestFor

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Customer)
class CustomerTests {

    //Req. C-1
    void testValidCustomer(){
        
        String email = 'kleczka@gmail.com'
        
        Customer c = new Customer(email: email, password: '1234567', enabled: true)
        c.save(flush: true)

        assert c.validate()
        Customer d = Customer.findByEmail(email)
        
        assert d.email
        assert d.password
        assert d.dateCreated
    }
    
    //Req. C-1, C-3, C-4
    void testInvalidCustomers(){
    
        def badCustomers = [
                            new Customer(email: '@@', password: '123456', enabled: true),
                            new Customer(email: 'kleczka@gmail.com', password: '12345', enabled: true)
        ]
        
        badCustomers.each{
            it.save(flush: true)
            
            assert !it.validate()
            Customer d = Customer.findByEmail(it.email)
            assertNull d
        }
        
    }
    
    //Req. C-3
    void testValidEmails() {
        
        def goodEmails = ['kleczka@gmail.com','paul@kleczka.museum','kleczka@a.b.c.d.com']
        
        goodEmails.each{ 
            Customer c = new Customer(email:  it, password:  '123456', enabled: true)
            c.save(flush: true)
            assert c.validate()
            assertNull c.errors['email']
        }
    }

    //Req. C-3
    void testInvalidEmails() {
       
        def badEmails = ['nothing','@gmail.com','paul@','paul@com','paul@gmail.','paul@.com']

        badEmails.each{ Customer c = new Customer(email: it, password: '123456')
                        c.save(flush: true)
                        assert !c.validate()
                        assertEquals 'email.invalid',c.errors['email'].code
        }
    }

    //Req. C-3
    void testBlankEmails() {
        
        def blankEmails = ['',' ','          ']
        
        blankEmails.each{Customer c = new Customer(email:  it, password: '123456')
                         c.save(flush: true)
                         assert !c.validate()
                         assertEquals 'blank',c.errors['email'].code
        }
    }

    //Req. C-3
    void testNullEmail(){
        
        Customer c = new Customer(password: '123456')
        c.save(flush: true)
        assert !c.validate()
        assertEquals 'nullable',c.errors['email'].code
    }

    //Req. C-4
    void testBlankPasswords(){
        
        def blankPasswords = ['',' ','        ']
        
        blankPasswords.each{ Customer c = new Customer(password:  it, email: "kleczka@yahoo.com")
                             c.save(flush: true)
                             assert !c.validate()
                             assertEquals 'blank',c.errors['password'].code
        }
    }

    //Req. C-4
    void testNullPassword(){
        
        Customer c = new Customer(email: 'kleczka@yahoo.com')
        c.save(flush: true)
        assert !c.validate()
        assertEquals 'nullable',c.errors['password'].code
    }

    //Req. C-4
    void testValidPasswords(){
        
        def goodPasswords = ['123456','1234567','12345678','abcdef','abcdefgh','!@#$%^&*','()?><:;"','{}|/*-']

        goodPasswords.each{ Customer c = new Customer(password: it, email: "kleczka@yahoo.com", enabled: true)
                            c.save(flush: true)
                            //don't check validate because unique password constraint will fail...
                            //assert c.validate()
                            assertNull c.errors['password']
        }
    }

    //Req. C-4
    void testShortPasswords(){
        
        def invalidPasswords = ['1','12345','a','!@#$%']
        
        invalidPasswords.each{ 
            Customer c = new Customer(password: it, email:  "kleczka@yahoo.com", enabled: true)
            c.save(flush: true)
            assert !c.validate()
            assertEquals 'minSize.notmet',c.errors['password'].code
        }
    }

    //Req. C-5
    //This test is no longer needed
    //With SpringSecurityService the passwords are encrypted in the database, meaning the column needs to be longer
    //than 8 chars.  The web form will not allow inputs longer than 8 characters.
    //A customer could be create a password longer than 8 chars if they bypassed the form, but really this would
    //only result in a more secure password....
    /*void testLongPasswords(){

        def invalidPasswords = ['123456789','{}|;"?/><>000000']

        invalidPasswords.each{
            Customer c = new Customer(password: it, email:  "kleczka@yahoo.com", enabled: true)
            c.save(flush: true)
            assert !c.validate()
            assertEquals 'size.toobig',c.errors['password'].code
        }
    }*/

    //Req. C-2
    void testUniquePasswords(){
        
        def password = '1234567'
        def email = "kleczka@gmail.com"
        
        Customer c1 = new Customer(password: password, email: email, enabled: true)
        Customer c2 = new Customer(password: password, email: email, enabled: true)

        c1.save(flush: true)
        assert c1.validate()
        assertFalse c1.errors.hasErrors()
        
        c2.save(flush: true)
        assert !c2.validate()
        assertEquals 'unique',c2.errors['email'].code
        
    }
}
